package com.sangmo.fts.process.factoring.service;

import com.sangmo.boot.framework.common.utils.DateUtils;
import com.sangmo.boot.framework.data.idm.IdentityManager;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.process.factoring.interfaces.FfRepaymentCreation;
import com.sangmo.fts.process.factoring.model.FfContract;
import com.sangmo.fts.process.factoring.model.FfLoan;
import com.sangmo.fts.process.factoring.model.FfRepayment;
import com.sangmo.fts.process.factoring.repo.FfRepaymentRepo;
import com.sangmo.fts.process.inventory.model.IfRepayment;
import com.sangmo.fts.training.model.Training;
import com.sangmo.fts.training.model.TrainingHistory;
import com.sangmo.fts.training.service.TrainingHistoryEntityService;
import com.sangmo.fts.training.service.TrainingService;
import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class FfRepaymentEntityService extends AuthorityEntityService<FfRepayment> {

    @Autowired
    private FfRepaymentRepo ffRepaymentRepo;

    @Autowired
    private IdentityManager identityManager;

    @Autowired
    private FfLoanService ffLoanService;

    @Autowired
    private FfContractService ffContractService;

    @Autowired
    private TrainingHistoryEntityService trainingHistoryEntityService;

    @Override
    protected IDataViewRepo<FfRepayment> getRepo() {
        return ffRepaymentRepo;
    }

    @Override
    protected void onCreate(FfRepayment entity, Object bo) {
        super.onCreate(entity, bo);
        FfRepaymentCreation creation = (FfRepaymentCreation) bo;
        entity.setCode(DateUtils.format(new Date(), "yyyyMMdd") + identityManager.next("FF_REPAYMENT", 4));
        FfLoan loan = ffLoanService.get(creation.getLoanId());
        FfContract contract = ffContractService.get(loan.getContractId());
        entity.setPayId(contract.getBuyerId());
        entity.setBankId(loan.getBankId());
        entity.setFinancingId(loan.getApplyId());
    }

    @Override
    protected void afterCreated(FfRepayment entity, Object bo) {
        super.afterCreated(entity, bo);
        List<FfRepayment> repaymentList = ffRepaymentRepo.findByLoanId(entity.getLoanId());
        BigDecimal totalPaymentAmount = BigDecimal.ZERO;
        for (FfRepayment repayment : repaymentList) {
            totalPaymentAmount = totalPaymentAmount.add(repayment.getAmount());
        }
        totalPaymentAmount = totalPaymentAmount.add(entity.getAmount());
        ffLoanService.settle(entity.getLoanId(), totalPaymentAmount);

        //到期未偿款
        FfLoan loan = ffLoanService.get(((FfRepaymentCreation)bo).getLoanId());
        Date now = new Date();
        if (now.compareTo(loan.getEndTime()) > 0) {
            TrainingHistory th = new TrainingHistory();
            th.setTrainingId(entity.getTrainingId());
            th.setProcessNode(TrainingHistory.FfProcessNodes.FF_LOAN_SELLER_PAY_15);
            trainingHistoryEntityService.create(th);
        }


    }
}
